<template>
    <div class="search">
        <div class="title search_top">
            <div class="ei">
                <el-icon>
                    <Search />
                </el-icon>
                <h3>筛选搜索</h3>
            </div>
            <div>
                <el-button @click="qingkong">清空</el-button>
                <el-button type="primary" @click="onSearch">查询搜索</el-button>
            </div>
        </div>
        <div class="search_input">
            <p>角色名称:</p>
            <el-input type="text" placeholder="请输入角色名称" v-model="name"></el-input>
        </div>
    </div>
    <div class="adddata">
        <div class="title">
            <el-icon>
                <Tickets />
            </el-icon>
            <h3>数据列表</h3>
        </div>

        <div>
            <el-button type="primary" @click="onAddUser">添加</el-button>
        </div>
    </div>
    <div class="tables">
        <!-- 数据的展示 -->
        <el-table align="center" :data="tableData" style="width: 100%" :border="true">
            <el-table-column align="center" prop="id" label="编号" width="60" />
            <el-table-column align="center" prop="name" label="角色名称" width="280" />
            <el-table-column align="center" prop="description" label="描述" width="300" />
            <el-table-column align="center" prop="adminCount" label="用户数" width="240" />
            <!-- 自定义列模板 -->
            <el-table-column align="center" label="添加时间" width="180">
                <!-- slot 插槽写法  scope就是作用域插槽 ， scope.row 就是数据 -->
                <template #default="scope">
                    <div>
                        {{ scope.row.createTime ? dayjs(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss') : '/' }}
                    </div>
                </template>
            </el-table-column>

            <el-table-column align="center" label="是否启用" width="240">
                <template #default="scope">
                    <el-switch :value="scope.row.status === 1 ? true : false" />
                </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="240" flex="right">
                <template #default="scope">
                    <el-button @click="btn(scope.row.id)">分配菜单</el-button>
                    <el-button @click="btnClick(scope.row.id)">分配资源</el-button>
                    <el-button @click="onEdit(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="pagination">
            <!-- 分页器 -->
            <el-pagination v-model:current-page="pagination.pageNum" v-model:page-size="pagination.pageSize" background
                :page-sizes="[1, 5, 10, 20]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
        <!-- 添加弹出框 -->
        <roleEdit :visible="visible" @cancel="onCloseDialog" @confirms="onConfirmDialog" :form="modalForm" />
    </div>
</template>
  
<script setup lang="ts">
import { Ref, ref } from 'vue'
import * as api from '../../request/api'
import useTable from '../../hooks/useTables'
import dayjs from 'dayjs'
import roleEdit from "../ums/roleAdd.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { IAdminListItem, IRoleListItem } from '../../request/QmsAdminConreillerA/index';
import { useRouter } from "vue-router";



let name = ref("")
let add = false;
let modalForm = ref({}) as Ref<IAdminListItem>;
let qingkong = () => {
    name.value = "";
}
let { tableData, getTableData, pagination, handleSizeChange, handleCurrentChange } = useTable(api.adminA.getAdminRole, {
    // keyword: name.value  // 可以删掉 name.value 不是一个响应式数据
    keyword: name // name 是响应式的
})
console.log(tableData);

// 添加
let visible = ref(false)
let onAddUser = () => {
    visible.value = true;
    add = true;
}
// 使用路由函数
const router = useRouter();
//分配角色
const btn = (id: any) => {
    router.push(
        { path: '/ums/allocMenu', query: { id } }
    )
};
//分配资源
const btnClick = (id: any) => {
    router.push(
        { path: '/ums/allocResource', query: { id } }
    )
}
// 编辑
let onEdit = (row: IAdminListItem) => {
    add = false;
    modalForm.value = row;
    visible.value = true;
};
// 列表数据请求函数调用
getTableData()
let onSearch = async () => {
    console.log(name.value);
    getTableData()
}

// 子级 emit组件传过来的事件
const onCloseDialog = () => {
    visible.value = false;
  };
  
  // 确认了输入框的内容之后
  const onConfirmDialog = (row: IAdminListItem) => {
    ElMessageBox.confirm("此操作将添加当前用户信息，是否继续？", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        if (add) {
          await api.admin.rolesRegister(row); //新增
        }
        ElMessage({
          type: "success",
          message: "添加成功",
        });
        visible.value = false;
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "您已取消",
        });
      });
  };
  
  //分配角色对话框显示隐藏控制
  let assign = ref(false);
  
  
</script>
  
<style lang="scss" scoped>
.search {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 20px 0;
    padding: 20px;
    flex-wrap: wrap;
    border: 1px solid #ebeef5;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);

    .search_top {
        width: 100%;
    }

    .search_input {
        display: flex;
        align-items: center;
        width: 100%;
        padding: 0 40px;
        padding-top: 20px;

        .el-input {
            width: 200px;
            height: 32px;
            font-size: 14px;
            padding: 0 10px;
        }

        p {
            width: 87px;
            height: 32px;
            line-height: 32px;
            font-size: 14px;
            color: #606266;
            padding: 0 12px 0 0;
            box-sizing: border-box;
        }
    }
}

.adddata {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid #ebeef5;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
}

.title {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .ei {
        display: flex;
        align-items: center;

        h3 {
            color: #303133;
            font-size: 14px;
            margin-left: 10px;
        }
    }

    h3 {
        color: #303133;
        font-size: 14px;
        margin-left: 10px;
    }
}

.tables {
    .pagination {
        margin: 20px 0;
        padding-right: 20px;
        float: right;
    }

    .users {
        color: #409eff;
        font-size: 12px;

        &:hover {
            color: rgba(64, 158, 255, 0.7);
            cursor: pointer;
        }
    }
}
</style>
  